package com.renjiyue.GUI;
//08事件监听改良
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//事件监听
public class GUIDome09 {
    public static void main(String[] args) {
        Frame frame = new Frame("TestActionEvent");
        frame.setSize(500,500);
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //创建一个监听对象
        MyMonitor monitor = new MyMonitor();
        //一个监听对象同时监听两个按钮的动作
        button1.addActionListener(monitor);
        button2.addActionListener(monitor);
        //设置btn2的执行单机命令后的返回信息
        button2.setActionCommand("GameOver");

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.CENTER);
        addwindowClosingEvent(frame);
        frame.setVisible(true);
    }
    private static void addwindowClosingEvent(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("A Button has been Pressed,"+"the relative info is:\n");
    }
}